<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            border: 1px solid black;
        }

        tr {
            border: 1px solid black;
        }

        td {
            border: 1px solid black;
        }

        th {
            border: 1px solid black;
        }
    </style>
</head>

<body>
    <input type="text" placeholder="请输入作者" name="aut">
    <button onclick="getBooksByName()">查找</button>
    <!-- 锚点：#返回页面顶部 -->
    <a href="./update.html">新增</a>
    <table id="tbl" cellspacing="0">
        <tr>
            <th>
                序号
            </th>
            <th>
                标题
            </th>
            <th>
                分类
            </th>
            <th>
                作者
            </th>
            <th>
                发布时间
            </th>
            <th>
                操作
            </th>
        </tr>

    </table>
    <script src="./js/jquery-3.6.1.min.js"></script>
    <script>
        //放在onready事件中

        getBookInfo();
        let tbl = $("#tbl");




        //渲染页面
        function getBookInfo() {
            $.get('http://localhost:3000/v1/books', (obj) => {
                showBooksInfo(obj);
            })
        }

        function showBooksInfo(obj) {
            // 清空
            let tr = $('.tr');
            tr.remove();

            console.log(obj.data);
            obj.data.forEach((element, index) => {
                const { id, name, sort, author, pubTime } = element;
                let html = $(`
                <tr class='tr'>
                    <td>${index + 1}</td>
                    <td>${name}</td>
                    <td>${sort}</td>
                    <td>${author}</td>
                    <td>${pubTime}</td>
                    <td>
                        <button onclick="onChange(${id})">修改</button>
                        <a href="#" onclick="onDelete(${id})">删除</a>
                    </td>
            </tr>
                `)
                tbl.append(html);
            });
        }

        // 根据作者查询书籍
        function getBooksByName() {
            // 获取输入的值
            let value = $('[name="aut"]').val();
            $.get(`http://localhost:3000/v1/books/aut/${value}`, (obj) => {
                showBooksInfo(obj);
            })
        }

        //根据ID删除书籍 get post put  delete
        function onDelete(id) {
            let isDelete = confirm("确定删除吗?");
            if (isDelete) {
                $.ajax({
                    url: `http://localhost:3000/v1/books/${id}`,
                    type: 'DELETE',
                    success: (obj) => {
                        alert('删除成功');
                        location.reload();
                    }
                })
            }
        }

        // 
        function onChange(id){
            location.href = `./update.html?id=${id}`
        }

    </script>
</body>

</html>